package the.broccoli.setting.interfaces.controller;

import io.swagger.annotations.ApiParam;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.wisteria.framework.web.APIResult;
import org.wisteria.framework.web.BusinessException;
import org.wisteria.framework.web.QueryResult;
import the.broccoli.auth.UserContext;
import the.broccoli.setting.dto.LanguageCondition;
import the.broccoli.setting.entity.Language;
import the.broccoli.setting.service.ILanguageService;

import javax.annotation.Resource;
import java.util.*;

/**
 * 多语言控制器
 *
 * @author lei.liu
 * @since 2020-04-18 13:53:46
 */
@RestController
@RequestMapping("/setting/language")
public class LanguageController {

    @Resource
    private ILanguageService languageService;

    @PostMapping("/add")
    public APIResult add(
            @ApiParam(name = "constant", value = "代码", required = false)
            @RequestParam(name = "constant", required = false) String constant,
            @ApiParam(name = "languageCode", value = "编码", required = false)
            @RequestParam(name = "languageCode", required = false) String languageCode,
            @ApiParam(name = "languageZhCn", value = "中文", required = false)
            @RequestParam(name = "languageZhCn", required = false) String languageZhCn,
            @ApiParam(name = "languageEnUs", value = "英文", required = false)
            @RequestParam(name = "languageEnUs", required = false) String languageEnUs
    ) {
        checkCode(languageCode);

        Language entity = new Language();
        entity.setConstant(constant);
        entity.setLanguageCode(languageCode);
        entity.setLanguageZhCn(languageZhCn);
        entity.setLanguageEnUs(languageEnUs);
        entity.setCreatedId(UserContext.userId());
        entity.setCreatedName(UserContext.userName());
        entity.setCreatedTime(new Date());
        languageService.save(entity);
        return APIResult.success();
    }

    @PostMapping("/remove")
    public APIResult remove(@RequestParam(value = "ids", required = true) List<String> ids) {
        languageService.removeByIds(ids, UserContext.userId(), UserContext.userName());
        return APIResult.success();
    }

    @PostMapping("/modify")
    public APIResult modify(
            @ApiParam(name = "languageId", value = "多语言ID", required = false)
            @RequestParam(name = "languageId", required = false) String languageId,
            @RequestParam(name = "modifiedName", required = false) String modifiedName,
            @ApiParam(name = "constant", value = "代码", required = false)
            @RequestParam(name = "constant", required = false) String constant,
            @ApiParam(name = "languageCode", value = "编码", required = false)
            @RequestParam(name = "languageCode", required = false) String languageCode,
            @ApiParam(name = "languageZhCn", value = "中文", required = false)
            @RequestParam(name = "languageZhCn", required = false) String languageZhCn,
            @ApiParam(name = "languageEnUs", value = "英文", required = false)
            @RequestParam(name = "languageEnUs", required = false) String languageEnUs
    ) {
        Language entity = new Language();
        entity.setLanguageId(languageId);
        entity.setModifiedName(modifiedName);
        entity.setConstant(constant);
        entity.setLanguageCode(languageCode);
        entity.setLanguageZhCn(languageZhCn);
        entity.setLanguageEnUs(languageEnUs);
        entity.setModifiedId(UserContext.userId());
        entity.setModifiedName(UserContext.userName());
        entity.setModifiedTime(new Date());
        languageService.updateById(entity);
        return APIResult.success();
    }

    @GetMapping("/{languageId}")
    public APIResult get(@PathVariable(value = "languageId") String languageId) {
        return APIResult.success(languageService.getById(languageId));
    }

    @GetMapping("/page")
    public APIResult<QueryResult> page(
            @ApiParam(name = "constant", value = "代码", required = false)
            @RequestParam(name = "constant", required = false) String constant,
            @ApiParam(name = "languageCode", value = "编码", required = false)
            @RequestParam(name = "languageCode", required = false) String languageCode,
            @ApiParam(name = "languageZhCn", value = "中文", required = false)
            @RequestParam(name = "languageZhCn", required = false) String languageZhCn,
            @ApiParam(name = "languageEnUs", value = "英文", required = false)
            @RequestParam(name = "languageEnUs", required = false) String languageEnUs,
            @ApiParam(name = "keyword", value = "关键字", required = false)
            @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(name = "orderBy", value = "排序字段", required = false)
            @RequestParam(value = "orderBy", required = false) String orderBy,
            @ApiParam(name = "pageNum", value = "页码", required = false, defaultValue = "1")
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @ApiParam(name = "pageNum", value = "每页大小", required = false, defaultValue = "20")
            @RequestParam(value = "pageSize", required = false) Integer pageSize
    ) {
        LanguageCondition condition = new LanguageCondition();
        condition.setConstant(constant);
        condition.setLanguageCodeLike(languageCode);
        condition.setLanguageZhCn(languageZhCn);
        condition.setLanguageEnUs(languageEnUs);
        condition.setKeyword(keyword);
        condition.setOrderBy(orderBy);
        return APIResult.success(languageService.pageList(condition, pageNum, pageSize));
    }

    @GetMapping("/json/{constant}")
    public APIResult list(
            @ApiParam(name = "constant", value = "代码", required = true)
            @PathVariable(value = "constant") String constant
    ) {
        Map<String, String> data = null;
        LanguageCondition condition = new LanguageCondition();
        condition.setConstant(constant);
        List<Language> list = languageService.listByCondition(condition);
        if (!CollectionUtils.isEmpty(list)) {
            data = new HashMap();
            for (Language entity : list) {
                String name = null;
                String lang = LocaleContextHolder.getLocale().getLanguage();
                if (Locale.US.getLanguage().equals(lang)) {
                    name = entity.getLanguageEnUs();
                } else {
                    name = entity.getLanguageZhCn();
                }
                data.put(entity.getLanguageCode(), name);
            }
        }
        return APIResult.success(data);
    }

    private void checkCode(String languageCode) {
        if (!StringUtils.hasText(languageCode)) {
            return;
        }
        LanguageCondition condition = new LanguageCondition();
        condition.setLanguageCode(languageCode);
        List<Language> list = languageService.listByCondition(condition);
        if (!CollectionUtils.isEmpty(list)) {
            throw BusinessException.withErrorCode("language.code.is.exists");
        }
    }
}